﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccessLayer;
using BusinessLogicLayer;

namespace ChamCong.Admin
{
    public partial class QuanLyLoaiNV : BasePage
    {
        LoaiNVBLL LNVBLL = new LoaiNVBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Huy();
            }
        }

        protected void btnThemCV_Click(object sender, EventArgs e)
        {
            LOAINV LNV = new LOAINV();
            LNV.MaLoaiNV = int.Parse(txtMaLoaiNV.Text);
            LNV.TenLoaiNV = txtTenLoaiNV.Text;
            LNV.MoTa = txtMoTa.Text;

            if (LNVBLL.KiemTraTonTai(LNV.MaLoaiNV, LNV.TenLoaiNV) != null)
                lbthongbao.Text = LNVBLL.KiemTraTonTai(LNV.MaLoaiNV, LNV.TenLoaiNV) + " đã tồn tại!";
            else
            {
                LNVBLL.ThemLoaiNV(LNV);
                dgvDSLoaiNV.DataBind();
            }
        }

        protected void DSLoaiNV_rowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ImageButton imgbtXoa = (ImageButton)e.Row.FindControl("imgbtXoa");
                ImageButton imgbtXem = (ImageButton)e.Row.FindControl("imgbtXem");

                if (imgbtXoa != null)
                    imgbtXoa.Attributes["MaLoaiNV"] = e.Row.Cells[0].Text;
                if (imgbtXem != null)
                    imgbtXem.Attributes["MaLoaiNV"] = e.Row.Cells[0].Text;
            }
        }

        protected void imgbtXoa_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton btn = (ImageButton)sender;
            int ma = int.Parse(btn.Attributes["MaLoaiNV"]);

            try
            {
                LNVBLL.Xoa(ma);
                dgvDSLoaiNV.DataBind();

                //dgvDSPhongBan.DataBind();
                //Reset();
            }
            catch
            {
                lbThongbao2.Text = "Không thể xóa!";
            }
        }

        protected void imgbtXem_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton btn = (ImageButton)sender;
            int ma = int.Parse(btn.Attributes["MaLoaiNV"]);
            try
            {
                LOAINV a = LNVBLL.LayLoaiNV(ma);
                txtMaLoaiNV.Text = a.MaLoaiNV.ToString();
                txtTenLoaiNV.Text = a.TenLoaiNV.ToString();
                txtMoTa.Text = a.MoTa;
                txtMaLoaiNV.Enabled = false;
                txtMoTa.Enabled = false;
                txtTenLoaiNV.Enabled = false;
            }
            catch { }
        }

        protected void btnHuy_Click(object sender, EventArgs e)
        {
            Huy();
        }

        protected void Huy()
        {
            txtMaLoaiNV.Text = "";
            txtTenLoaiNV.Text = "";
            txtMoTa.Text = "";
            txtMaLoaiNV.Enabled = true;
            txtMoTa.Enabled = true;
            txtTenLoaiNV.Enabled = true;
        }
    }
}